//
// Created by shiminli on 2024/1/3.
//

#ifndef DDOFFICE_ACCOUNTS_H
#define DDOFFICE_ACCOUNTS_H

#include <iostream>
#include "../SQLHelper.h"
#include "../SQLBuilder.h"
#include <mysql/mysql.h>
#include <vector>
#include <optional>
#include "hv/httpdef.h"
#include "hv/HttpService.h"

using namespace std;
using namespace ddoffice;
/**
 CREATE TABLE `accounts` (
  `user_id` bigint unsigned NOT NULL,
  `account` char(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `password` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `phone_number` varchar(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `area_code` varchar(8) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `email` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `nickname` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `face_url` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci DEFAULT NULL,
  `gender` int DEFAULT NULL,
  `create_time` datetime(3) DEFAULT NULL,
  `change_time` datetime(3) DEFAULT NULL,
  `birth_time` datetime(3) DEFAULT NULL,
  `level` int DEFAULT '1',
  `allow_vibration` int DEFAULT '1',
  `allow_beep` int DEFAULT '1',
  `allow_add_friend` int DEFAULT '1',
  `global_recv_msg_opt` int DEFAULT '0',
  PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
 */
struct Accounts {
    long user_id;
    string account;
    string password;
    string phone_number;
    string area_code;
    string email;
    string nickname;
    string face_url;
    int gender;
    string create_time;
    string change_time;
    string birth_time;
    int level = 1;
    int allow_vibration = 1;
    int allow_beep = 1;
    int allow_add_friend = 1;
    int global_recv_msg_opt = 0;
    // 将结构体转换为JSON
    void to_json(hv::Json& j) const {
        j = hv::Json{
                {"user_id", user_id},
                {"account", account},
                {"password", password},
                {"phone_number", phone_number},
                {"area_code", area_code},
                {"email", email},
                {"nickname", nickname},
                {"face_url", face_url},
                {"gender", gender},
//                {"create_time", create_time},
//                {"change_time", change_time},
                {"birth_time", birth_time},
//                {"level", level},
//                {"allow_vibration", allow_vibration},
//                {"allow_beep", allow_beep},
//                {"allow_add_friend", allow_add_friend},
//                {"global_recv_msg_opt", global_recv_msg_opt}
        };
    }

    // 将JSON转换为结构体
    void from_json(const hv::Json& j) {
        j.at("user_id").get_to(user_id);
        j.at("account").get_to(account);
        j.at("password").get_to(password);
        j.at("phone_number").get_to(phone_number);
        j.at("area_code").get_to(area_code);
        j.at("email").get_to(email);
        j.at("nickname").get_to(nickname);
        j.at("face_url").get_to(face_url);
        j.at("gender").get_to(gender);
        j.at("create_time").get_to(create_time);
        j.at("change_time").get_to(change_time);
        j.at("birth_time").get_to(birth_time);
        j.at("level").get_to(level);
        j.at("allow_vibration").get_to(allow_vibration);
        j.at("allow_beep").get_to(allow_beep);
        j.at("allow_add_friend").get_to(allow_add_friend);
        j.at("global_recv_msg_opt").get_to(global_recv_msg_opt);
    }


};
struct AccountsColumnName {
    const string tabName = "accounts";
    const string user_id = "user_id";
    const string account = "account";
    const string password = "password";
    const string phone_number = "phone_number";
    const string area_code = "area_code";
    const string email = "email";
    const string nickname = "nickname";
    const string face_url = "face_url";
    const string gender = "gender";
    const string create_time = "create_time";
    const string change_time = "change_time";
    const string birth_time = "birth_time";
    const string level = "level";
    const string allow_vibration = "allow_vibration";
    const string allow_beep = "allow_beep";
    const string allow_add_friend = "allow_add_friend";
    const string global_recv_msg_opt = "global_recv_msg_opt";

};
class accounts {
public:
    static AccountsColumnName names;
    static vector<string> columns ;
    static int insert(Accounts &data);

    int update();

    vector<Accounts> queryAll();

    static std::optional<Accounts> queryById(const long &user_id);
    static std::optional<Accounts> queryByPhoneNumber(const string &phone_number);
    static long queryUserIdByPhoneNumber(const string &phone_number);
    static void rowToStruct(const MYSQL_ROW &row,Accounts &acc);

};


#endif //DDOFFICE_ACCOUNTS_H
